﻿using JinYuan.Helper;
using JinYuan.MES;
using JinYuan.MES.Models;
using JinYuan.Models;
using JinYuan.VirtualDataLibrary;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace JinYuan.ControlCenters
{
    public class MESDataCombin
    {




        #region 9、报警信息
        /// <summary>
        /// 报警信息
        /// </summary>
        /// <param name="alarmObj">报警内容</param>
        /// <param name="alarmFlag">报警标志 1:报警开始  0：报警结束</param>
        /// <returns></returns>
        public static string EquAlarm(AlarmData alarmObj, int alarmFlag)
        {
            DateTime nowTime = DateTime.Now;
            string resJson = "";
            string josnTxtMsg = string.Empty;
            try
            {
                UploadAlarmParam alarmParam = new UploadAlarmParam();
                alarmParam.siteCode = CommonMethods.mesConfig.siteCode;
                alarmParam.lineCode = CommonMethods.mesConfig.lineCode;
                alarmParam.equipNum = CommonMethods.mesConfig.equipNum;
                alarmParam.materialCode = CommonMethods.mesConfig.MaterialCode;
                alarmParam.userName = CommonMethods.mesConfig.mesUserName;
                alarmParam.recordDate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                alarmParam.guid = alarmObj.AlarmGuid;
                alarmParam.seatId = "";
                List<AlarmLineListItem> alarmLineList = new List<AlarmLineListItem>();

                AlarmLineListItem alarmList = new AlarmLineListItem();
                alarmList.alarmEndTime = alarmFlag == 1 ? "" : alarmObj.EndTime.ToString();
                alarmList.alarmType = alarmObj.AlarmType;
                alarmList.alarmName = alarmObj.AlarmContent;
                alarmList.alarmStartTime = alarmObj.StartTime.ToString();
                alarmList.faultCode = alarmObj.AlarmCode;
                alarmLineList.Add(alarmList);
                alarmParam.alarmLineList = alarmLineList;
                string SendJosn = JsonConvert.SerializeObject(alarmParam);
                var sw = new Stopwatch();
                sw.Start();
                resJson = MESApiHelper.HttpPostJsonAPI(CommonMethods.mesConfig.AlarmCurrencyMesUrl, SendJosn);
                sw.Stop();
                josnTxtMsg = $"{nowTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用MES设备报警接口:{CommonMethods.mesConfig.AlarmCurrencyMesUrl},请求数据为:{SendJosn},\n{nowTime.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resJson}";
                TxtHelper.WriteTxt($@"{CommonMethods.strMesLogspath}\{nowTime.ToString("yyyyMMdd")}\设备报警\{nowTime.ToString("HH")}.txt", josnTxtMsg);
            }
            catch (Exception ex)
            {
                LoggerHelp.WriteEX("调用MES设备报警接口", ex);
            }
            return resJson;

        }
        #endregion
        #region 10、设备状态
        /// <summary>
        /// 设备状态
        /// </summary>
        /// <param name="strStatus"></param>
        /// <returns></returns>
        public static string EqpStatusParam(string strStatus, string StartDate, string strGuid, List<FaultCodeList> FaultCodeLists = null)
        {
            var nowTime = DateTime.Now;
            string resJson = "";
            string josnTxtMsg = string.Empty;
            string Date = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            try
            {
                EqpStatusParam statusParam = new EqpStatusParam();
                statusParam.siteCode = CommonMethods.mesConfig.siteCode;
                statusParam.lineCode = CommonMethods.mesConfig.lineCode;
                statusParam.equipNum = CommonMethods.mesConfig.equipNum;
                statusParam.userName = CommonMethods.mesConfig.mesUserName;
                statusParam.seatId = "";
                statusParam.recordDate = StartDate;
                statusParam.statusCode = strStatus;
                statusParam.uploadTime = Date;
                statusParam.guid = strGuid;
                statusParam.faultCodeList = FaultCodeLists;
                string SendJosn = JsonConvert.SerializeObject(statusParam);

                var sw = new Stopwatch();
                sw.Start();
                resJson = MESApiHelper.HttpPostJsonAPI(CommonMethods.mesConfig.StatusMesUrl, SendJosn);
                sw.Stop();
                josnTxtMsg = $"{nowTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用MES设备运行状态接口:{CommonMethods.mesConfig.StatusMesUrl},请求数据为:{SendJosn},\n{nowTime.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resJson}";
                TxtHelper.WriteTxt($@"{CommonMethods.strMesLogspath}\{nowTime.ToString("yyyyMMdd")}\设备运行状态\{nowTime.ToString("HH")}.txt", josnTxtMsg);
            }
            catch (Exception ex)
            {
                LoggerHelp.WriteEX("调用MES设备运行状态接口", ex);
            }
            return resJson;
        }

        #endregion


    }
}
